/**
 * 
 */
package org.gov.vis.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.gui.VisJFrame;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author Vishwanath
 *
 */
public class LoginListener implements ActionListener {

	 VISLoginDialog visLgnDialog;
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private static VisJFrame mainFrame;

	public LoginListener(VISLoginDialog visLoginDialog){
		this.visLgnDialog = visLoginDialog;

	}//end Constructor

	@Override
	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		errorMsgPanel = new ErrorsDisplayJPanel(visLgnDialog.getErrorPanel());
		errorMsgPanel.clearMsgPanel();


		if(ae.getActionCommand().equals("Login")){
			List<String> errorMsgs = new ArrayList<String>();
			String _visUsrErr = ValidatorUtils.validateString(rb.getString("login_username"), visLgnDialog.getTxtUsrName().getText(), 5, 15, true);
			if(_visUsrErr != null)
				errorMsgs.add( _visUsrErr);

			String _visPswErr = ValidatorUtils.validateString(rb.getString("login_password"), visLgnDialog.getTxtPassword().getText(), 5, 15, true);
			if(_visUsrErr != null)
				errorMsgs.add( _visPswErr);

			if(errorMsgs.isEmpty()){
				
				if(!((visLgnDialog.getTxtUsrName().getText().equals(GuiConstants.LOGIN_USER_NAME)) && (visLgnDialog.getTxtPassword().getText().equals(GuiConstants.LOGIN_PASSWORD))))
				{
					errorMsgs.add("Invalid User Name or Pasaword");

				}else {
					
					mainFrame = new VisJFrame("VIS");
					visLgnDialog.dispose();
				}
			}
			if(!errorMsgs.isEmpty()){
				String loginErrorMsgs = "";
				for(String errMsg : errorMsgs){
					loginErrorMsgs += errMsg;
					if(!errMsg.contains("\n")){
						loginErrorMsgs += "\n";
					}
				}
				JOptionPane.showMessageDialog(visLgnDialog, loginErrorMsgs);
			}

		}//end if
		else {
			
			clear();
		}
	}//end ActionPerformed

	public void clear() {
		visLgnDialog.getTxtUsrName().setText("");
		visLgnDialog.getTxtPassword().setText("");
	}//end clear

}
